<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="pdictionaryTypeing-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getDictionaryDetails">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="dictionaryDetails" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>

			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="username" label="姓名" width="120" sortable>
			</el-table-column>
			<el-table-column prop="operation" label="执行操作" width="120" sortable>
			</el-table-column>
			<el-table-column prop="time" label="运行时间" width="160" sortable>
			</el-table-column>
			<el-table-column prop="method" label="方法" width="120" sortable>
			</el-table-column>
			<el-table-column prop="ip" label="IP地址" width="100" sortable>
			</el-table-column>
			<el-table-column prop="create_time" label="时间" width="100" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :current-page="currentPage"
						   :page-size="pageSize"
						   :total="total"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增或编辑-->
		<el-dialog :title="title" :visible.sync="dictionaryTypeFormVisible" :close-on-click-modal="false">
			<el-form :model="dictionaryTypeForm" label-width="80px" :rules="dictionaryTypeFormRules" ref="dictionaryTypeForm">
				<el-form-item label="品牌名" prop="name">
					<el-input v-model="dictionaryTypeForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="类型" prop="types_id">
					<!--<el-input v-model="dictionaryTypeForm.types_id" auto-complete="off"></el-input>-->
					<el-select v-model="dictionaryTypeForm.types" value-key="id" placeholder="请选择">
						<el-option
								v-for="item in dictionaryTypes"
								:key="item.id"
								:label="item.name"
								:value="item">
							<span style="float: left">{{ item.name }}</span>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="dictionaryTypeFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="dictionaryTypeSubmit" :loading="dictionaryTypeLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>

</template>

<script>
    export default {
        data() {
            return {
                filters: {
                    keyword: ''
                },
                dictionaryTypes:[],
                dictionaryDetails: [],
                total: 0,
                currentPage: 1,
                pageSize:10,
                title:"",
                listLoading: false,
                sels: [],//列表选中列

                dictionaryTypeFormVisible: false,//新增界面是否显示
                dictionaryTypeLoading: false,
                dictionaryTypeFormRules: {
                    name: [
                        { required: true, message: '请输入名称', trigger: 'blur' }
                    ],
                    sn: [
                        { required: true, message: '请输入编号', trigger: 'blur' }
                    ]
                },
                //新增界面数据
                dictionaryTypeForm: {
                    types_id:null,
                    name: '',
                }

            }
        },
        methods: {
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getDictionaryDetails();
            },
            //获取员工列表
            getDictionaryDetails() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize:this.pageSize,
                    keyword: this.filters.keyword
                };
                this.listLoading = true;
                this.$http.post("/log",para).then(result=>{
                    this.listLoading = false;
                    result = result.data;
                    this.dictionaryDetails = result.rows;
                    this.total = result.total;
                }).catch(result=>{
                    this.$message({
                        message: '操作失败',
                        type: 'error'
                    });
                })
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true; // 开启忙等框
                    this.$http.delete("/log/"+row.id).then(result=>{
                        this.listLoading=false;  // 关闭忙等框
                        result = result.data;  // AjaxResult
                        if (result.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                        this.currentPage=1;
                        this.getDictionaryDetails();
                    }).catch(result=>{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    })

                }).catch(() => {
                    this.$message({
                        message: '取消操作',
                        type: 'error'
                    });
                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.dictionaryTypeForm = Object.assign({}, row);
                this.title="编辑";
                this.dictionaryTypeFormVisible = true;
                this.dictionaryTypeForm.parent = [2,7];
            },
            //显示新增界面
            handleAdd: function () {
                this.title="新增";
                this.dictionaryTypeFormVisible = true;
                // 清空历史数据
                this.dictionaryTypeForm = {
                    types_id:null,
                    name: '',
                };
            },
            //新增/编辑
            dictionaryTypeSubmit: function () {
                this.$refs.dictionaryTypeForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.dictionaryTypeLoading = true;  // 开启提交按钮忙等框
                            let para = Object.assign({}, this.dictionaryTypeForm);
                            this.$http.put("/log",para).then(result=>{
                                result=result.data;
                                this.dictionaryTypeLoading=false;
                                if (result.success){
                                    this.dictionaryTypeFormVisible = false;//关闭弹出的新增或修改模态框
                                    this.$message({
                                        message: '操作成功',
                                        type: 'success'
                                    });
                                }else {
                                    this.$message({
                                        message: '操作失败',
                                        type: 'error'
                                    });
                                }
                                this.currentPage=1;
                                this.getDictionaryDetails();
                            }).catch(result=>{
                                this.dictionaryTypeLoading=false;
                                this.$message({
                                    message: '失败',
                                    type: 'error'
                                });
                            })
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.patch("/log",ids).then(result=>{
                        this.listLoading=false;
                        result=result.data;
                        if(result.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                        this.currentPage=1;
                        this.getDictionaryDetails();
                    }).catch(result=>{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    })
                }).catch(() => {
                    this.$message({
                        message: '取消操作',
                        type: 'error'
                    });
                });
            },

            // 获取部门管理员
            getDictionaryTypes(){
                this.$http.get("/type").then(result=>{
                    result=result.data;
                    this.dictionaryTypes=result;
                }).catch(result=>{
                    this.$message({
                        message: '员工获取失败',
                        type: 'error'
                    });
                })
            },

        },
        mounted() {
            this.getDictionaryDetails();
            this.getDictionaryTypes()
        }
    }

</script>

<style scoped>

</style>